// 所有一级页面均需引入skinMixIn，在页面的最顶级元素中设置css变量，skinMixIn包含AppSkin与AppSkinVar，AppSkin为当前设置的皮肤，AppSkinVar返回一个皮肤对象，对象内包含所有颜色的css变量
import skinMixIn from './skinMixIn'
// #ifdef PRIVATE || SUBSCRIPTION
import {
	wxAppMessageShareData, wxTimelineShareData,
	getShareLink
} from '@/common/wxInit'
// #endif

export default {
	mixins: [skinMixIn],
	data() {
		return {
			componentsList: [],
			pageOption: {},
			customPageBgImgUrl: '',
			customPageBgImgHeight: 0,
			isDefaultShare: true,
			mpShare: {}
		}
	},
	computed: {
		pageStyle() {
			let data = {}
			if (this.pageOption.styleType == 1) {
				data = {
					padding: `${this.pageOption.paddingTop * 2}rpx ${this.pageOption.paddingX * 2}rpx  ${this.pageOption.paddingBottom * 2}rpx ${this.pageOption.paddingX * 2}rpx`
				}
			}
			if (this.pageOption.type === '1') {
				return { ...data, backgroundColor: this.pageOption.backgroundColor }
			}
			if (this.pageOption.type === '2' && this.pageOption.backgroundUrl || this.customPageBgImgUrl) {
				return {
					...data,
					backgroundImage: `url(${this.$fullLink(this.customPageBgImgUrl || this.pageOption.backgroundUrl)})`,
					backgroundRepeat: 'no-repeat',
					backgroundSize: this.customPageBgImgUrl ? `100% ${this.customPageBgImgHeight}px` : '100%',
					transition: this.customPageBgImgUrl ? 'background 0.6s ease-in-out' : ''
				}
			}
			return data
		},
		style() {
			return { ...this.pageStyle, ...this.AppSkinVar }
		}
	},
	methods: {
		setMpShare(data) {
			let { title, desc, imgUrl, appParams = '', link = '' } = data
			// #ifdef APP-PLUS
			this.mpShare = {
				title,
				appDesc: desc,
				appParams,
				imageUrl: imgUrl // 默认为当前页面的截图
			}
			uni.$endless.mpShare = this.mpShare
			// #endif
			// #ifdef MP-WEIXIN||MP-ALIPAY
			this.mpShare = {
				title, // 默认为小程序名称
				desc,
				path: '', // 默认为当前页面路径
				imageUrl: imgUrl // 默认为当前页面的截图
			}
			uni.$endless.mpShare = this.mpShare
			// #endif

			// #ifdef PRIVATE || SUBSCRIPTION
			this.mpShare = {
				title,
				desc,
				link,
				imgUrl
			}
			uni.$endless.mpShare = this.mpShare
			wxAppMessageShareData(uni.$endless.mpShare)
			wxTimelineShareData(uni.$endless.mpShare)
			// #endif
		},
		setPageBackgroundImage(data) {
			this.customPageBgImgUrl = data.url
			this.customPageBgImgHeight = data.height
		},
		setPageOption(data) {
			if (!data?.pageLayout) return
			uni.setNavigationBarTitle({
				title: data?.pageName || '\u200e'
			})
			this.componentsList.splice(0, this.componentsList.length)
			let arr = ['page', 'share']
			JSON.parse(data.pageLayout).forEach((item, index) => {
				if (arr.includes(item.componentsName)) {
					switch (item.componentsName) {
						case 'page':
							this.pageOption = item
							if (item.headerColor) {
								let headerColor = item.headerColor
									.replace(/rgba?\(/, '')
									.replace(/\)/, '')
									.replace(/[\s+]/g, '')
									.split(',')

								let a = parseFloat(headerColor[3] || 1),
									r = Math.floor(a * parseInt(headerColor[0]) + (1 - a) * 255),
									g = Math.floor(a * parseInt(headerColor[1]) + (1 - a) * 255),
									b = Math.floor(a * parseInt(headerColor[2]) + (1 - a) * 255)

								uni.setNavigationBarColor({
									frontColor: item.headerTextColor,
									backgroundColor: `#${('0' + r.toString(16)).slice(-2)}${('0' + g.toString(16)).slice(-2)}${('0' + b.toString(16)).slice(-2)}`
								})
							}

							break
						case 'share':
							if (item.type === '1') {
								this.isDefaultShare = false
								let link = ''
								let { title, desc, imgUrl } = item
								// #ifdef MP-WEIXIN||MP-ALIPAY
								imgUrl = item.mpType === '1' ? item.mpImgUrl : '' // 默认为当前页面的截图
								// #endif

								// #ifdef PRIVATE || SUBSCRIPTION
								link = getShareLink()
								// #endif

								this.setMpShare({
									title,
									desc,
									imgUrl,
									link
								})
							}
							break
					}
				} else {
					if (!item._componentsHidden) {
						this.componentsList.push({
							key: index + item.componentsName,
							components: item.componentsName,
							option: item
						})
					}
				}
			})
		}
	}
}